﻿//-----------------------------------------------------------------------
// <copyright file="RelayCommand.cs" author="Artak Mkrtchyan">
//     Creative Commons - GNU General Public License (http://creativecommons.org/licenses/GPL/2.0/
// </copyright>
// <author>Artak Mkrtchyan</author>
// <email>mkArtak@gmail.com</email>
// <date>15/07/2012</date>
//-----------------------------------------------------------------------
using ConsoleHoster.Common.Utilities;
using System;
using System.Windows.Input;

namespace ConsoleHoster.Common.ViewModel
{
	public class RelayCommand : ICommand
	{
		public static RelayCommand EmptyCommand = new RelayCommand(p =>
		{
		});

		#region Fields
		private readonly Action<object> commandAction;
		private readonly Predicate<object> canExecute;
		#endregion

		#region Constructors
		public RelayCommand(Action<object> execute)
			: this(execute, null)
		{
		}

		public RelayCommand(Action<object> argCommandAction, Predicate<object> argCanExecute)
		{
			ValidationManager.RequireArgumentNotNull(argCommandAction, "argCommandAction");

			this.commandAction = argCommandAction;
			this.canExecute = argCanExecute;
		}
		#endregion

		#region ICommand Members
		public bool CanExecute(object argParameter)
		{
			return this.canExecute == null ? true : this.canExecute(argParameter);
		}

		public event EventHandler CanExecuteChanged
		{
			add
			{
				CommandManager.RequerySuggested += value;
			}
			remove
			{
				CommandManager.RequerySuggested -= value;
			}
		}

		public void Execute(object argParameter)
		{
			this.commandAction(argParameter);
		}
		#endregion
	}
}